<?php

namespace app\model;

use think\facade\Db;
use think\Model;

class Group extends Model
{
    protected $table = 'group';

    public static function add($name, $uid)
    {
        Db::startTrans();//开启事件
        if ($group_id = self::insert(['name' => $name, 'uid' => $uid], 'id')) {
            if (GroupUser::insert(['group_id' => $group_id, 'uid' => $uid])) {
                Db::commit(); // 提交事务
                return $group_id;
            } else {
                Db::rollback();// 回滚事务
                return false;
            }
        } else {
            Db::rollback();// 回滚事务
            return false;
        }
    }
}